{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a7effa1a",
   "metadata": {},
   "source": [
    "**2022/10/31 礼仪的week11**"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1ff58eac",
   "metadata": {},
   "source": [
    "# 本周学习知识点\n",
    "> **元组、集合、列表 的基本概念和常见方法**\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d77a5130",
   "metadata": {},
   "source": [
    "# 一.元组（tuple）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b684ab12",
   "metadata": {},
   "source": [
    "> 1.概念：具有和序列数据相同的计算规则：取值和切片\n",
    ">\n",
    "> 2.表征：（12345,54321，‘hello！’），用圆括号/小括号 括起来的，中间依然是用逗号隔开的数据形态\n",
    ">\n",
    "> 3.性质：不可变性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "2ed7cc24",
   "metadata": {},
   "outputs": [],
   "source": [
    "product=(\"head first python\",88)# 创建了一个组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "ce44434e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('head first python', 88) <class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "print(product,type(product))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "5e770874",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 案例\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "a5df1c90",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2022, tm_mon=12, tm_mday=24, tm_hour=1, tm_min=46, tm_sec=35, tm_wday=5, tm_yday=358, tm_isdst=0)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.localtime()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "26d5578e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on struct_time object:\n",
      "\n",
      "class struct_time(builtins.tuple)\n",
      " |  struct_time(iterable=(), /)\n",
      " |  \n",
      " |  The time value as returned by gmtime(), localtime(), and strptime(), and\n",
      " |  accepted by asctime(), mktime() and strftime().  May be considered as a\n",
      " |  sequence of 9 integers.\n",
      " |  \n",
      " |  Note that several fields' values are not the same as those defined by\n",
      " |  the C language standard for struct tm.  For example, the value of the\n",
      " |  field tm_year is the actual year, not year - 1900.  See individual\n",
      " |  fields' descriptions for details.\n",
      " |  \n",
      " |  Method resolution order:\n",
      " |      struct_time\n",
      " |      builtins.tuple\n",
      " |      builtins.object\n",
      " |  \n",
      " |  Methods defined here:\n",
      " |  \n",
      " |  __reduce__(...)\n",
      " |      Helper for pickle.\n",
      " |  \n",
      " |  __repr__(self, /)\n",
      " |      Return repr(self).\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Static methods defined here:\n",
      " |  \n",
      " |  __new__(*args, **kwargs) from builtins.type\n",
      " |      Create and return a new object.  See help(type) for accurate signature.\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Data descriptors defined here:\n",
      " |  \n",
      " |  tm_gmtoff\n",
      " |      offset from UTC in seconds\n",
      " |  \n",
      " |  tm_hour\n",
      " |      hours, range [0, 23]\n",
      " |  \n",
      " |  tm_isdst\n",
      " |      1 if summer time is in effect, 0 if not, and -1 if unknown\n",
      " |  \n",
      " |  tm_mday\n",
      " |      day of month, range [1, 31]\n",
      " |  \n",
      " |  tm_min\n",
      " |      minutes, range [0, 59]\n",
      " |  \n",
      " |  tm_mon\n",
      " |      month of year, range [1, 12]\n",
      " |  \n",
      " |  tm_sec\n",
      " |      seconds, range [0, 61])\n",
      " |  \n",
      " |  tm_wday\n",
      " |      day of week, range [0, 6], Monday is 0\n",
      " |  \n",
      " |  tm_yday\n",
      " |      day of year, range [1, 366]\n",
      " |  \n",
      " |  tm_year\n",
      " |      year, for example, 1993\n",
      " |  \n",
      " |  tm_zone\n",
      " |      abbreviation of timezone name\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Data and other attributes defined here:\n",
      " |  \n",
      " |  n_fields = 11\n",
      " |  \n",
      " |  n_sequence_fields = 9\n",
      " |  \n",
      " |  n_unnamed_fields = 0\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Methods inherited from builtins.tuple:\n",
      " |  \n",
      " |  __add__(self, value, /)\n",
      " |      Return self+value.\n",
      " |  \n",
      " |  __contains__(self, key, /)\n",
      " |      Return key in self.\n",
      " |  \n",
      " |  __eq__(self, value, /)\n",
      " |      Return self==value.\n",
      " |  \n",
      " |  __ge__(self, value, /)\n",
      " |      Return self>=value.\n",
      " |  \n",
      " |  __getattribute__(self, name, /)\n",
      " |      Return getattr(self, name).\n",
      " |  \n",
      " |  __getitem__(self, key, /)\n",
      " |      Return self[key].\n",
      " |  \n",
      " |  __getnewargs__(self, /)\n",
      " |  \n",
      " |  __gt__(self, value, /)\n",
      " |      Return self>value.\n",
      " |  \n",
      " |  __hash__(self, /)\n",
      " |      Return hash(self).\n",
      " |  \n",
      " |  __iter__(self, /)\n",
      " |      Implement iter(self).\n",
      " |  \n",
      " |  __le__(self, value, /)\n",
      " |      Return self<=value.\n",
      " |  \n",
      " |  __len__(self, /)\n",
      " |      Return len(self).\n",
      " |  \n",
      " |  __lt__(self, value, /)\n",
      " |      Return self<value.\n",
      " |  \n",
      " |  __mul__(self, value, /)\n",
      " |      Return self*value.\n",
      " |  \n",
      " |  __ne__(self, value, /)\n",
      " |      Return self!=value.\n",
      " |  \n",
      " |  __rmul__(self, value, /)\n",
      " |      Return value*self.\n",
      " |  \n",
      " |  count(self, value, /)\n",
      " |      Return number of occurrences of value.\n",
      " |  \n",
      " |  index(self, value, start=0, stop=9223372036854775807, /)\n",
      " |      Return first index of value.\n",
      " |      \n",
      " |      Raises ValueError if the value is not present.\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Class methods inherited from builtins.tuple:\n",
      " |  \n",
      " |  __class_getitem__(...) from builtins.type\n",
      " |      See PEP 585\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(time.localtime())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "cc1f09ab",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.localtime().tm_wday\n",
    "time.localtime().tm_hour\n",
    "time.localtime().tm_sec"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cb755840",
   "metadata": {},
   "source": [
    "# 二.集合（sets）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6f48e101",
   "metadata": {},
   "source": [
    "> 集合的概念：没有重复的数据结构\n",
    ">\n",
    ">> 形态：{'apple','orange','apple','banaba','orange'}\n",
    ">>\n",
    ">> 集合的运算 1.交集运算：& ,intersection 2.并集运算：| ,union"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "d9de4583",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'orange', 'apple', 'banaba'} <class 'set'>\n"
     ]
    }
   ],
   "source": [
    "basket = {'apple','orange','apple','banaba','orange'}\n",
    "print(basket,type(basket))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "514af665",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "舞蹈社 = {1001,1002,1003,1004,1005}\n",
    "合唱团 = {1004,1005,1005,1007}\n",
    "len(舞蹈社) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "5d017d2c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(合唱团)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "be4d3f5c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1001, 1002, 1003, 1004, 1005} <class 'set'>\n"
     ]
    }
   ],
   "source": [
    "print(舞蹈社,type(舞蹈社) )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "26849b98",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1001, 1002, 1003, 1004, 1005}\n"
     ]
    }
   ],
   "source": [
    "print(舞蹈社)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "2f7b2f83",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1004, 1005}\n",
      "{1001, 1002, 1003, 1004, 1005, 1007}\n",
      "{1004, 1005}\n",
      "{1001, 1002, 1003, 1004, 1005, 1007}\n"
     ]
    }
   ],
   "source": [
    "# 交集 并集\n",
    "print(舞蹈社 & 合唱团)\n",
    "print(舞蹈社 | 合唱团)\n",
    "print(舞蹈社.intersection(合唱团) )\n",
    "print(舞蹈社.union(合唱团) )"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cf5b4fea",
   "metadata": {},
   "source": [
    "# 三.字典（dict）\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2955bede",
   "metadata": {},
   "source": [
    "> 1.基本的概念：\n",
    ">\n",
    ">> A. mapping types :映射类型\n",
    ">>\n",
    ">> B. key:values,键：值 对"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "856c2066",
   "metadata": {},
   "source": [
    "> 2.字典的创建：\n",
    ">\n",
    ">> 2.1 无序性\n",
    ">>\n",
    ">> 2.2 key 不可变性\n",
    ">>\n",
    ">> 2.3 key唯一性\n",
    ">\n",
    "> 3.常用的方法："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "62e777cf",
   "metadata": {},
   "outputs": [],
   "source": [
    "product_list={\n",
    "    \"rice\":1,\n",
    "    \"vegetable\":2,\n",
    "    \"beef\":6\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "43c1fe13",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'rice': 1, 'vegetable': 2, 'beef': 6} <class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "print(product_list,type(product_list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "efacee74",
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "0",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "Input \u001b[1;32mIn [21]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43mproduct_list\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m]\u001b[49m\n",
      "\u001b[1;31mKeyError\u001b[0m: 0"
     ]
    }
   ],
   "source": [
    "product_list[0] # 字典是无序性的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "fc83ccac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "product_list[\"rice\"]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
